package com.study.login.dao;

import com.study.common.BaseDao.BaseDaoImpl;
import com.study.common.entity.LoginException;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.login.entity.Identity;
import com.study.login.entity.Jurisdiction;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @Create by ldr.
 * @create 2018-08-28 14:20
 * @Description：
 */
@Repository(value = "loginDao")
public class LoginDaoImpl extends BaseDaoImpl implements LoginDao {


    @Override
    public User getUserByUserName(String userName) {
        String hql = "from User where userName = ?";
        List list = new ArrayList();
        list.add(userName);
        return (User) queryUniqueByHql(hql,list);
    }

    @Override
    public Identity getIdentityByUser(String userName) {
        String hql = "from Identity i where i.identityId in " +
                "(select u.identityId from User u where u.userName = ?)";
        List list = new ArrayList();
        list.add(userName);
        return (Identity) queryUniqueByHql(hql,list);
    }

    @Override
    public List<Jurisdiction> getJurisdictionById(int identityId) {
        Session s = getSession();
        String hql = "from Jurisdiction j where j.jurisdictionId in" +
                "(select ij.jurisdictionId from IdAndJur ij where ij.identityId = ?)";
        Query q = s.createQuery(hql);
        q.setParameter(0,identityId);
        return q.list();

    }
}
